package com.domcer.ultra.menu.application.listener;

import com.domcer.ultra.menu.application.cache.PlayerCache;
import com.domcer.ultra.menu.domain.MenuDomain;
import com.domcer.ultra.menu.domain.MenuItemDomain;
import com.domcer.ultra.menu.engine.ActionManager;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;

import java.util.Map;

/**
 * @Author hang.wang
 * @Date 2025/1/31 12:16
 */
public class InventoryClickListener implements Listener {

    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        HumanEntity humanEntity = event.getWhoClicked();
        if (humanEntity instanceof Player) {
            Player player = (Player) humanEntity;
            MenuDomain menuDomain = PlayerCache.getMenuDomain(player);
            if (menuDomain != null) {
                event.setCancelled(true);
                Map<Integer, MenuItemDomain> itemsBySlot = menuDomain.getItemsBySlot();
                int slot = event.getSlot();
                MenuItemDomain menuItemDomain = itemsBySlot.get(slot);
                ClickType clickType = event.getClick();
                if (menuItemDomain != null) {
                    ActionManager.execute(player, menuItemDomain.getClickActions());
//                if (clickType.isLeftClick()) {
//                } else if (clickType.isRightClick()) {
//                }
                }
            }
        }
    }
}
